package com.fuyin.commons.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description 拦截mybatis更新数据，设gmt_modified为null
 * @date 2021年10月24日 3:03 下午
 * @since
 */

@Intercepts({
        @Signature(type = Executor.class, method = GmtModifiedInterceptor.METHOD_UPDATE, args = {MappedStatement.class, Object.class})
})
public class GmtModifiedInterceptor implements Interceptor {
    private final static Logger logger = LoggerFactory.getLogger(GmtModifiedInterceptor.class);

    public final static String METHOD_UPDATE = "update";

    public final static String[] METHOD_SET_GMT_MODIFIED = new String[]{"setGmtModified"};

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
//        String methodName = invocation.getMethod().getName();
//        if (methodName.equalsIgnoreCase(GmtModifiedInterceptor.METHOD_UPDATE)) {
//            Object param = invocation.getArgs()[1];
//            try {
//                Field gmtModified = param.getClass().getDeclaredField("gmtModified");
//                gmtModified.setAccessible(true);
//                gmtModified.set(param, null);
//            } catch (Exception e) {
//                logger.warn("setGmtModified error:" + e.getMessage() + ", class:" + param.getClass());
//            }
//        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

